package cn.edu.henu.team.ui_login;

import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.material.textfield.TextInputLayout;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnFocusChange;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;
import cn.edu.henu.team.R;
import cn.edu.henu.team.base.BaseActivity;
import cn.edu.henu.team.bean._User;
import cn.edu.henu.team.constant.ModelConstant;
import cn.edu.henu.team.fragments.HomeActivity;
import cn.edu.henu.team.widget.OwlView;
import cn.edu.henu.team.utils.RegexUtils;

public class LoginActivity extends BaseActivity {

    @BindView(R.id.login_owl_view)
    OwlView mOwlView;
    @BindView(R.id.login_et_phone)
    TextInputLayout userphone;
    @BindView(R.id.login_et_password)
    EditText passwordET;
    @BindView(R.id.login_tv_forget)
    TextView forget;
    @BindView(R.id.login_tv_sign)
    TextView signTV;
    @BindView(R.id.login_tv_phone)
    TextView loginphoneTV;
    @BindView(R.id.login_btn_login)
    Button loginBtn;
    @BindView(R.id.login_bottom_iv_qq)
    ImageView QQLogin;
    @BindView(R.id.login_bottom_iv_wechat)
    ImageView WeChatLogin;

    private EditText mEdtPhone;
    // Log打印的通用Tag
    private final String TAG = "LoginActivity";

    @Override
    protected int initLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        _User user = _User.getCurrentUser(_User.class);
        if(user!=null){
            startActivity(new Intent(LoginActivity.this,HomeActivity.class));
            finish();
        }
        mEdtPhone = userphone.getEditText();
        //设置监听对输入的数据进行合法性校验
        mEdtPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String phone = charSequence.toString();
                if (!RegexUtils.checkPhone(phone)) {
                    mEdtPhone.setError("请输入正确的手机号");
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });

        passwordET.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String password = s.toString();
                if (!RegexUtils.checkPassword(password)) {
                    passwordET.setError("请输入正确的密码");
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    //监听密码输入框的聚焦事件
    @OnFocusChange({R.id.login_et_password,R.id.login_btn_login})
    public void onFocusChange(View view, boolean b) {
        if (b) {
            mOwlView.open();
        } else {
            mOwlView.close();
        }
    }

    /**
     * 监听点击事件
     *
     * @param view
     */
    @OnClick({R.id.login_tv_sign, R.id.login_btn_login, R.id.login_tv_forget, R.id.login_tv_phone, R.id.login_bottom_iv_qq, R.id.login_bottom_iv_wechat})
    protected void onClick(View view) {

        String telephone = mEdtPhone.getText().toString();
        String password = passwordET.getText().toString();

        switch (view.getId()) {
            case R.id.login_btn_login:  //登录
                if (!(RegexUtils.checkPhone(telephone) && RegexUtils.checkPassword(password))) {
                    Toast.makeText(this, "账号或密码错误", Toast.LENGTH_SHORT).show();
                    break;
                }
                asyncLogin(telephone, password);
                finish();
                break;
            case R.id.login_tv_forget:  //忘记密码
                startActivity(new Intent(mContext, ForgetPasswordActivity.class));
                break;
            case R.id.login_tv_sign:  //sign
                startActivity(new Intent(mContext, RegisterActivity.class));
                finish();
                break;
            case R.id.login_tv_phone:  //手机号验证码登录
                startActivity(new Intent(mContext, OtpLoginActivity.class));
                finish();
                break;
            case R.id.login_bottom_iv_qq:

                break;

            case R.id.login_bottom_iv_wechat:

                break;
            default:
                break;
        }
    }

    protected void asyncLogin(String telephone,String password){
        _User user = new _User();
        user.setUsername(telephone);
        user.setPassword(password);
        user.login(new SaveListener<_User>() {
            @Override
            public void done(_User s, BmobException e) {
                if (e == null) {
                    insertVal(ModelConstant.LOGIN_INFO,"telephone",telephone);
                    insertVal(ModelConstant.LOGIN_INFO,"password",password);
                    startActivity(new Intent(LoginActivity.this, HomeActivity.class));
                    finish();
                } else {
                    if (e.getErrorCode() == 101)
                        Toast.makeText(getApplication(), "手机号或密码错误", Toast.LENGTH_SHORT).show();
                    else
                        Toast.makeText(getApplication(), "登录失败："+e.getMessage(), Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}